<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="./css/style.css" />
  </head>
  <body>
    <div class="parent">
      <h2>图片容器</h2>
      <div>
        <button id="btn">设置图片</button>
      </div>
      <div class="image">
        <img src="https://fygtest-1304421005.cos.ap-nanjing.myqcloud.com/banner/20.jpeg" alt="" />
      </div>
      <div class="image">
        <img src="https://fygtest-1304421005.cos.ap-nanjing.myqcloud.com/banner/21.jpeg" alt="" />
      </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
      $(function () {
        $('#btn').click(function () {
          // 替换 .image 的内容
          $('.parent')
            .find('.image')
            .html(function (index, oldcontent) {
              console.log('旧内容', index, oldcontent)
              // log 的内容
              /*
              旧内容 0
              <img src="https://fygtest-1304421005.cos.ap-nanjing.myqcloud.com/banner/20.jpeg" alt="">
              旧内容 1
              <img src="https://fygtest-1304421005.cos.ap-nanjing.myqcloud.com/banner/21.jpeg" alt="">
              */
              switch (index) {
                case 0:
                  return '<b>是第1个图</b>'
                case 1:
                  return '<i>是第2个图</i>'
              }
            })
        })
      })
    </script>
  </body>
</html>
